<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微信回调页面</title>

    <!--导入vue，axios和common(前缀路径以及).js,但是要记得vue导入要放在axios前面，因为axios底层是vue-->
    <script type="text/javascript" src="plugins/vue/dist/vue.js"></script>
    <script type="text/javascript" src="plugins/axios/dist/axios.js"></script>
    <script type="text/javascript" src="script/common.js"></script>
</head>
<body>
    <div id="callBackDiv">

    </div>
    <script type="text/javascript">
        new Vue({
            el:"#callBackDiv",
            data:{
                code:"",
            },
            methods:{
                wxLogin(){
                    this.$http.post("/login/wechat/"+this.code)
                    .then(result=>{
                        result=result.data
                        if (result.success){
                            if (result.resultObj.message&&result.resultObj.message=="noBind"){
                                //说明当前微信用户是第一次登录，需要跳转到绑定页面
                                //拿到后端传过来的accessToken,openid，跳转页面的时候将它们携带过去
                                let{accessToken,openid}=result.resultObj
                                location.href="/binder.html?accessToken="+accessToken+"&openid="+openid;
                            }else{
                                //说明登录成功，拿到后端返回的token和loginUser并且存入localstorage
                                alert("登录成功");
                                //common.js的前置拦截器已经在每次发起axios请求之前把token放入了请求头
                                let{token,loginUser}=result.resultObj

                                localStorage.setItem("token",token)
                                localStorage.setItem("loginUser",JSON.stringify(loginUser))
                                location.href="/index.html"
                            }
                        }else{
                            alert("微信登录失败"+result.msg)
                        }
                    })
                },

            },
            mounted(){
                //获取url中的code，并携带code调用后端接口
                let obj=url2Obj()
                this.code=obj.code
                this.wxLogin();
            }
        })
    </script>
</body>
</html>